package com.ruoyi.web.controller.ccc3gc;

import com.ccc3gc.mysystem.VO.StaffVO;
import com.ccc3gc.mysystem.bean.Staff;
import com.ccc3gc.mysystem.bean.User;
import com.ccc3gc.mysystem.service.AuthenticationService;
import com.ccc3gc.mysystem.service.StaffService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/api/staff")
public class StaffController extends BaseController {
    @Autowired
    StaffService staffService;
    @Autowired
    AuthenticationService authenticationService;

    //全部派送员列表
    @GetMapping("/list")
    public TableDataInfo list(Staff staff){
        startPage();
        List<Staff> list = staffService.selectStaffList(staff);
        return getDataTable(list);
    }

    //根据Id获取配送员
    @GetMapping("/queryStaff/{staffId}")
    public AjaxResult queryStaffById(@PathVariable int staffId){
        Staff staff = staffService.SelectStaffById(staffId);
        return AjaxResult.success(staff);
    }

    //获取配送员详细信息
    @GetMapping("/staffInfo/{staffId}")
    public AjaxResult staffInfoById(@PathVariable int staffId){
        StaffVO staffVO = staffService.staffInfoById(staffId);
        return AjaxResult.success(staffVO);
    }

    //添加配送员
    @PostMapping("/addStaff")
    public AjaxResult addStaff(Staff staff){
        int rows = staffService.instertStaff(staff);
        if (rows>0){
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    //修改配送员信息
    @PostMapping("/updateStaff")
    public AjaxResult updateStaff(@RequestBody Staff staff){
        int rows = staffService.updateStaff(staff);
        if(rows>0){
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    //根据Id删除配送员信息
    @DeleteMapping("/{staffId}")
    public AjaxResult deleteStaff(@PathVariable int staffId){
        Staff staff = staffService.SelectStaffById(staffId);
        int rows = staffService.deleteStaffById(staffId);
        //删除对应的认证信息
        authenticationService.deleteAuthenticationById(staff.getAuthId());
        if (rows>0){
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    //批量删除
    @DeleteMapping("/deleteStaffBatch/{staffIds}")
     public AjaxResult deleteStaffBatch(@PathVariable List<Integer> staffIds){
        List<Integer> authIds =new ArrayList<>();
        for (int staffId:staffIds) {
            Staff staff = staffService.SelectStaffById(staffId);
            authIds.add(staff.getAuthId());
        }
        int rows = staffService.deleteStaffByIds(staffIds);
        //批量删除对应的认证信息
        authenticationService.deleteAuthenticationByIds(authIds);
        if (rows>0){
            return AjaxResult.success();
        }
        return AjaxResult.error();
     }

    //导出
    @PostMapping("/export")
    public void export(HttpServletResponse response, Staff staff){
        List<Staff> list = staffService.selectStaffList(staff);
        ExcelUtil<Staff> util = new ExcelUtil<Staff>(Staff.class);
        util.exportExcel(response, list, "配送员数据");
    }
}
